Prompt caching
Prompt caching is a powerful feature that optimizes your API usage by allowing resuming from specific prefixes in your prompts. This approach significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements.
Here’s an example of how to implement prompt caching with the Messages API using a cache_control
block:
In this example, the entire text of “Pride and Prejudice” is cached using the cache_control
parameter. This enables reuse of this large text across multiple API calls without reprocessing it each time. Changing only the user message allows you to ask various questions about the book while utilizing the cached content, leading to faster responses and improved efficiency.
How prompt caching works
When you send a request with prompt caching enabled:
- The system checks if a prompt prefix, up to a specified cache breakpoint, is already cached from a recent query.
- If found, it uses the cached version, reducing processing time and costs.
- Otherwise, it processes the full prompt and caches the prefix once the response begins.
This is especially useful for:
- Prompts with many examples
- Large amounts of context or background information
- Repetitive tasks with consistent instructions
- Long multi-turn conversations
By default, the cache has a 5-minute lifetime. The cache is refreshed for no additional cost each time the cached content is used.
Prompt caching caches the full prefix
Prompt caching references the entire prompt - tools
, system
, and messages
(in that order) up to and including the block designated with cache_control
.
Pricing
Prompt caching introduces a new pricing structure. The table below shows the price per million tokens for each supported model:
Model | Base Input Tokens | 5m Cache Writes | 1h Cache Writes | Cache Hits & Refreshes | Output Tokens |
---|---|---|---|---|---|
Claude Opus 4 | $15 / MTok | $18.75 / MTok | $30 / MTok | $1.50 / MTok | $75 / MTok |
Claude Sonnet 4 | $3 / MTok | $3.75 / MTok | $6 / MTok | $0.30 / MTok | $15 / MTok |
Claude Sonnet 3.7 | $3 / MTok | $3.75 / MTok | $6 / MTok | $0.30 / MTok | $15 / MTok |
Claude Sonnet 3.5 | $3 / MTok | $3.75 / MTok | $6 / MTok | $0.30 / MTok | $15 / MTok |
Claude Haiku 3.5 | $0.80 / MTok | $1 / MTok | $1.6 / MTok | $0.08 / MTok | $4 / MTok |
Claude Opus 3 | $15 / MTok | $18.75 / MTok | $30 / MTok | $1.50 / MTok | $75 / MTok |
Claude Haiku 3 | $0.25 / MTok | $0.30 / MTok | $0.50 / MTok | $0.03 / MTok | $1.25 / MTok |
Note:
- 5-minute cache write tokens are 1.25 times the base input tokens price
- 1-hour cache write tokens are 2 times the base input tokens price
- Cache read tokens are 0.1 times the base input tokens price
- Regular input and output tokens are priced at standard rates
How to implement prompt caching
Supported models
Prompt caching is currently supported on:
- Claude Opus 4
- Claude Sonnet 4
- Claude Sonnet 3.7
- Claude Sonnet 3.5
- Claude Haiku 3.5
- Claude Haiku 3
- Claude Opus 3
Structuring your prompt
Place static content (tool definitions, system instructions, context, examples) at the beginning of your prompt. Mark the end of the reusable content for caching using the cache_control
parameter.
Cache prefixes are created in the following order: tools
, system
, then messages
.
Using the cache_control
parameter, you can define up to 4 cache breakpoints, allowing you to cache different reusable sections separately. For each breakpoint, the system will automatically check for cache hits at previous positions and use the longest matching prefix if one is found.
Cache limitations
The minimum cacheable prompt length is:
- 1024 tokens for Claude Opus 4, Claude Sonnet 4, Claude Sonnet 3.7, Claude Sonnet 3.5 and Claude Opus 3
- 2048 tokens for Claude Haiku 3.5 and Claude Haiku 3
Shorter prompts cannot be cached, even if marked with cache_control
. Any requests to cache fewer than this number of tokens will be processed without caching. To see if a prompt was cached, see the response usage fields.
For concurrent requests, note that a cache entry only becomes available after the first response begins. If you need cache hits for parallel requests, wait for the first response before sending subsequent requests.
Currently, “ephemeral” is the only supported cache type, which by default has a 5-minute lifetime.
What can be cached
Most blocks in the request can be designated for caching with cache_control
. This includes:
- Tools: Tool definitions in the
tools
array - System messages: Content blocks in the
system
array - Text messages: Content blocks in the
messages.content
array, for both user and assistant turns - Images & Documents: Content blocks in the
messages.content
array, in user turns - Tool use and tool results: Content blocks in the
messages.content
array, in both user and assistant turns
Each of these elements can be marked with cache_control
to enable caching for that portion of the request.
What cannot be cached
While most request blocks can be cached, there are some exceptions:
-
Thinking blocks cannot be cached directly with
cache_control
. However, thinking blocks CAN be cached alongside other content when they appear in previous assistant turns. When cached this way, they DO count as input tokens when read from cache. -
Sub-content blocks (like citations) themselves cannot be cached directly. Instead, cache the top-level block.
In the case of citations, the top-level document content blocks that serve as the source material for citations can be cached. This allows you to use prompt caching with citations effectively by caching the documents that citations will reference.
-
Empty text blocks cannot be cached.
Tracking cache performance
Monitor cache performance using these API response fields, within usage
in the response (or message_start
event if streaming):
cache_creation_input_tokens
: Number of tokens written to the cache when creating a new entry.cache_read_input_tokens
: Number of tokens retrieved from the cache for this request.input_tokens
: Number of input tokens which were not read from or used to create a cache.
Best practices for effective caching
To optimize prompt caching performance:
- Cache stable, reusable content like system instructions, background information, large contexts, or frequent tool definitions.
- Place cached content at the prompt’s beginning for best performance.
- Use cache breakpoints strategically to separate different cacheable prefix sections.
- Regularly analyze cache hit rates and adjust your strategy as needed.
Optimizing for different use cases
Tailor your prompt caching strategy to your scenario:
- Conversational agents: Reduce cost and latency for extended conversations, especially those with long instructions or uploaded documents.
- Coding assistants: Improve autocomplete and codebase Q&A by keeping relevant sections or a summarized version of the codebase in the prompt.
- Large document processing: Incorporate complete long-form material including images in your prompt without increasing response latency.
- Detailed instruction sets: Share extensive lists of instructions, procedures, and examples to fine-tune Claude’s responses. Developers often include an example or two in the prompt, but with prompt caching you can get even better performance by including 20+ diverse examples of high quality answers.
- Agentic tool use: Enhance performance for scenarios involving multiple tool calls and iterative code changes, where each step typically requires a new API call.
- Talk to books, papers, documentation, podcast transcripts, and other longform content: Bring any knowledge base alive by embedding the entire document(s) into the prompt, and letting users ask it questions.
Troubleshooting common issues
If experiencing unexpected behavior:
- Ensure cached sections are identical and marked with cache_control in the same locations across calls
- Check that calls are made within the cache lifetime (5 minutes by default)
- Verify that
tool_choice
and image usage remain consistent between calls - Validate that you are caching at least the minimum number of tokens
- While the system will attempt to use previously cached content at positions prior to a cache breakpoint, you may use an additional
cache_control
parameter to guarantee cache lookup on previous portions of the prompt, which may be useful for queries with very long lists of content blocks
Note that changes to tool_choice
or the presence/absence of images anywhere in the prompt will invalidate the cache, requiring a new cache entry to be created.
Caching with thinking blocks
When using extended thinking with prompt caching, thinking blocks have special behavior:
Automatic caching alongside other content: While thinking blocks cannot be explicitly marked with cache_control
, they get cached as part of the request content when you make subsequent API calls with tool results. This commonly happens during tool use when you pass thinking blocks back to continue the conversation.
Input token counting: When thinking blocks are read from cache, they count as input tokens in your usage metrics. This is important for cost calculation and token budgeting.
Cache invalidation patterns:
- Cache remains valid when only tool results are provided as user messages
- Cache gets invalidated when non-tool-result user content is added, causing all previous thinking blocks to be stripped
- This caching behavior occurs even without explicit
cache_control
markers
Example with tool use:
When a non-tool-result user block is included, it designates a new assistant loop and all previous thinking blocks are removed from context.
For more detailed information, see the extended thinking documentation.
Cache storage and sharing
-
Organization Isolation: Caches are isolated between organizations. Different organizations never share caches, even if they use identical prompts.
-
Exact Matching: Cache hits require 100% identical prompt segments, including all text and images up to and including the block marked with cache control.
-
Output Token Generation: Prompt caching has no effect on output token generation. The response you receive will be identical to what you would get if prompt caching was not used.
1-hour cache duration (beta)
If you find that 5 minutes is too short, Anthropic also offers a 1-hour cache duration.
To use the extended cache, add extended-cache-ttl-2025-04-11
as a beta header to your request, and then include ttl
in the cache_control
definition like this:
The response will include detailed cache information like the following:
Note that the current cache_creation_input_tokens
field equals the sum of the values in the cache_creation
object.
When to use the 1-hour cache
If you have prompts that are used at a regular cadence (i.e., system prompts that are used more frequently than every 5 minutes), continue to use the 5-minute cache, since this will continue to be refreshed at no additional charge.
The 1-hour cache is best used in the following scenarios:
- When you have prompts that are likely used less frequently than 5 minutes, but more frequently than every hour. For example, when an agentic side-agent will take longer than 5 minutes, or when storing a long chat conversation with a user and you generally expect that user may not respond in the next 5 minutes.
- When latency is important and your follow up prompts may be sent beyond 5 minutes.
- When you want to improve your rate limit utilization, since cache hits are not deducted against your rate limit.
The 5-minute and 1-hour cache behave the same with respect to latency. You will generally see improved time-to-first-token for long documents.
Mixing different TTLs
You can use both 1-hour and 5-minute cache controls in the same request, but with an important constraint: Cache entries with longer TTL must appear before shorter TTLs (i.e., a 1-hour cache entry must appear before any 5-minute cache entries).
When mixing TTLs, we determine three billing locations in your prompt:
- Position
A
: The token count at the highest cache hit (or 0 if no hits). - Position
B
: The token count at the highest 1-hourcache_control
block afterA
(or equalsA
if none exist). - Position
C
: The token count at the lastcache_control
block.
If B
and/or C
are larger than A
, they will necessarily be cache misses, because A
is the highest cache hit.
You’ll be charged for:
- Cache read tokens for
A
. - 1-hour cache write tokens for
(B - A)
. - 5-minute cache write tokens for
(C - B)
.
Here are 3 examples. This depicts the input tokens of 3 requests, each of which has different cache hits and cache misses. Each has a different calculated pricing, shown in the colored boxes, as a result.
Prompt caching examples
To help you get started with prompt caching, we’ve prepared a prompt caching cookbook with detailed examples and best practices.
Below, we’ve included several code snippets that showcase various prompt caching patterns. These examples demonstrate how to implement caching in different scenarios, helping you understand the practical applications of this feature: